package com.xunaan.util;

import com.xunaan.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MyBatisTest {
    SqlSession session;
    UserMapper mapper;

//    public static void main(String[] args) {
//        //1、创建SqlSessionFactory对象，也是单例模式的
//        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
//        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
//        SqlSession session = factory.openSession(true);
//        //3、调用session的方法namespace的名字.id
//        List<User> users = session.selectList("user.selectAllUser");
//        //4、打印测试
//        for(User u:users){
//            System.out.println(u);
//        }
//        //5、关闭资源s
//        session.close();
//    }
    @Before
    public void before(){
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        session = factory.openSession(true);
        mapper = session.getMapper(UserMapper.class);
    }

    @After
    public void after(){
        session.close();
    }

//    @Test
//    public void selectAllUser(){
//        List<User> users = mapper.selectAllUser();
//
//        for (User user : users) {
//            System.out.println(user);
//        }
//    }
//
//    @Test
//    public void insertUser(){
//        User user = new User();
//        user.setName("asdfasd");
//        user.setSal(100);
//        user.setBirthday(new Date());
//        mapper.insertUser(user);
//    }
//
//    @Test
//    public void deleteUserById(){
//        int id = 3;
//        mapper.deleteUserById(id);
//    }
//
//    @Test
//    public void updateUser(){
//        mapper.updateUser(4,"aaaaa");
//    }
@Test
public void selectAll(){
    for (User user : mapper.selectAll()) {
        System.out.println(user);
    }
}

    @Test
    public void selectByUser(){
        User u=new User();
//        u.setUid(1);
//        u.setUsername("a");
//        u.setPhone("a");
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUserOne(){
        User u=new User();
//        u.setUid(1);
        u.setName("a");
//        u.setPhone("a");
        for (User user : mapper.selectByUserOne(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void updateUser(){
        User u=new User();
        u.setId(1);
        u.setName("a");
        u.setPhone("a");
        u.setPassword("a");
        mapper.updateUser(u);
    }

    @Test
    public void trimTest(){

        mapper.trimTest();
    }
    @Test
    public void deleteByArray(){
        int[] array={4,5,6,7,8,9,10,11,12,13,14,15,16};
        mapper.deleteByArray(array);
    }
    @Test
    public void insertList(){
        List<User> list=new ArrayList<>();
        list.add(new User("a","a","a"));
        list.add(new User("a1","a1","a1"));
        list.add(new User("a2","a2","a2"));
        list.add(new User("a3","a3","a3"));
        list.add(new User("a4","a4","a4"));
        mapper.insertList(list);
    }
}
